package com.lanou.activity.utils;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

/**
 * 小工具
 */
public class ToolUtil {

    /**
     * 有参请求方式
     * @param url params
     * @return result 返回map集合
     */
    public static Object callUrl(String url,Map<String,Object> params){
        RestTemplate restTemplate=new RestTemplate();
        MultiValueMap<String,Object> bodyMap=new LinkedMultiValueMap<String,Object>();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            bodyMap.add(entry.getKey(),entry.getValue());
        }
        Object result = restTemplate.postForObject(url, bodyMap, Object.class);
        return result;
    }
    /**
     * 请求方式
     * @param url
     * @return 返回map集合
     */
    public static Object  callUrl(String url){
        RestTemplate restTemplate=new RestTemplate();
        MultiValueMap<String,Object> bodyMap=new LinkedMultiValueMap<String,Object>();
        Object result = restTemplate.postForObject(url, bodyMap, Object.class);
        return result;
    }


    /**
     * 工具1生成时间戳
     * 返回20位字符串 20170917080207673404
     */
    public static String dataTool(){
        String string =  "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        string = sdf.format(new Date());
        return string;
    }


    /**
     * 账号生成器
     */
    public static String AccountGenerator(String account){
        String vipNo = account+dataTool();
        Random r = new Random();
        for(int i =0 ;i<3;i++){
            vipNo=vipNo + r.nextInt(10);
        }
        return vipNo;
    }

    /**
     * 当前时间生成器
     */
    public static String dataToolDate(){
        String string =  "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        string = sdf.format(new Date());
        return string;
    }



}
